/**
 * 扬尘警报预警参数配置dustAlarmConfig.js
 * 版本号： v1.0.0
 */
$(function () {

    //查询公司树绑定
    bindCombo_companyId("companyId_");

    /**
     * 扬尘警报预警参数配置列表
     */
    var lastChecked = null;
    $("#dustAlarmConfig_dataList").datagrid({
        fit: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        striped: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/dustAlarmConfig/dustAlarmConfigList.do',
        idField: 'dacId',
        frozenColumns: [[
            {field: 'dacId', checkbox: true},
            {field: 'company', title: '公司名称', width: 200, align: 'left',
                formatter:function(value){
                    if(value){
                        return value.name
                    }
                }
            },
            {field: 'project', title: '项目名称', width: 200, align: 'left',
                formatter:function(value){
                    if(value){
                        return value.prName
                    }
                }
            },
        ]],
        columns: [
            [
                {field: 'earlyWarnTrigger', title: '预警触发持续时间(s)', width: 150, align: 'center'},
                {field: 'alarmTrigger', title: '报警触发持续时间(s)', width: 150, align: 'center'},
                {field: 'aqiEarlyWarn', title: 'aqi预警阈值(μg/m³)', width: 150, align: 'center'},
                {field: 'aqiAlarm', title: 'aqi报警阈值(μg/m³)', width: 150, align: 'center'},
                {field: 'pm25EarlyWarn', title: 'pm2.5预警阈值(μg/m³)', width: 150, align: 'center'},
                {field: 'pm25Alarm', title: 'pm2.5报警阈值(μg/m³)', width: 150, align: 'center'},
                {field: 'pm10EarlyWarn', title: 'pm10预警阈值(μg/m³)', width: 150, align: 'center'},
                {field: 'pm10Alarm', title: 'pm10报警阈值(μg/m³)', width: 150, align: 'center'},
                {field: 'wsEarlyWarn', title: '风速预警阈值(m/s)', width: 150, align: 'center'},
                {field: 'wsAlarm', title: '风速报警阈值(m/s)', width: 150, align: 'center'},
                {field: 'tempEarlyWarn', title: '温度预警阈值(°C)', width: 150, align: 'center'},
                {field: 'tempAlarm', title: '温度报警阈值(°C)', width: 150, align: 'center'},
                {field: 'humEarlyWarn', title: '湿度预警阈值(%rh)', width: 150, align: 'center'},
                {field: 'humAlarm', title: '湿度报警阈值(%rh)', width: 150, align: 'center'},
                {field: 'noiseEarlyWarn', title: '噪声预警阈值(db)', width: 150, align: 'center'},
                {field: 'noiseAlarm', title: '噪声报警阈值(db)', width: 150, align: 'center'}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#dustAlarmConfig_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#dustAlarmConfig_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#dustAlarmConfig_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#dustAlarmConfig_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            dustAlarm_add();
                        }
                    });
                }
                //修改按钮
                if("editBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            var checked = $("#dustAlarmConfig_dataList").treegrid("getChecked");
                            if (!checked || checked.length == 0) {
                                $.messager.show({ title: '操作提示', msg: "请选择一条数据后再进行修改操作", timeout: 3000, showType: "slide" });
                                return;
                            };
                            var row = $("#dustAlarmConfig_dataList").datagrid("getSelected");
                            var dacId = row.dacId;
                            window.location.href = CONTEXT_PATH + "/dustAlarmConfig/toDustAlarmConfig.do?id="+dacId;
                        }
                    });
                }
                //删除按钮
                if("delBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            dustAlarm_del();
                        }
                    });
                }
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            dustAlarmConfig_doSearch();
                        }
                    });
                }
                //详情按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            dustAlarmConfig_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 添加警报预警参数
 */
function dustAlarm_add(){
    window.location.href = CONTEXT_PATH + "/dustAlarmConfig/toDustAlarmConfig.do";
}


/**
 * 删除警报预警参数
 */
function dustAlarm_del() {
    var checked = $("#dustAlarmConfig_dataList").treegrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择要删除的警报参数", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#dustAlarmConfig_dataList").datagrid("getSelected");
    $.messager.confirm('删除提示', '您确定要删除 ' + row.company.name + ' 下的配置参数吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/dustAlarmConfig/delDustAlarmConfig.do?id=" + row.dacId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#dustAlarmConfig_dataList").datagrid("reload", {
                        prId: $("#prId_").combobox("getValue"),
                        companyId: $("#companyId_").combobox("getValue")
                    });
                }
            })
        }
    });
}



/**
 * 执行查询
 */
function dustAlarmConfig_doSearch() {
    //刷新dg页码
    refreshPageNumber("dustAlarmConfig_dataList");
    $("#dustAlarmConfig_dataList").datagrid("reload", {
        prId: $("#prId_").combobox("getValue"),
        companyId: $("#companyId_").combobox("getValue")
    });
    $("#dustAlarmConfig_dataList").datagrid("clearSelections");
    $('#dustAlarmConfigQuery_dialog').dialog('close');
}


/**
 * 查看字典
 */
function dustAlarmConfig_view(iconCls){
    var row = $("#dustAlarmConfig_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的警报详情", timeout: 3000, showType: "slide" });
        return;
    }
    var row = $("#dustAlarmConfig_dataList").datagrid("getSelected");
    //IFRAM打开窗口
    layoutSub(ycjbpzxq.id,ycjbpzxq.url+ row.dacId, iconCls, ycjbpzxq.closable,ycjbpzxq.title);
}

/**
 * 公司树bind事件
 */
function bindCombo_companyId() {
    // 公司下拉树
    $('#companyId_').combotree({
        url: CONTEXT_PATH + "/company/getTreeCompany.do",
        onSelect:function(node){
            var companyId = node.id;
            $("#prId_").combobox("clear");
            // 级联项目
            $.ajax({
                url: CONTEXT_PATH + "/project/getProjectListForCombo.do?companyId="+companyId,
                type: "get",
                success: function (data) {
                    var obj = JSON.parse(data);
                    var projectCombo = [{'text': '--请选择--', 'value': ''}];
                    for (var i = 0; i < obj.length; i++) {
                        projectCombo.push({"text": obj[i].prName, "value": obj[i].prId});
                    }
                    $("#prId_").combobox("loadData", projectCombo);
                }
            })
        }
    })
}






